var InoutRecoderEdit = function ( targetObj ) {
    this.targetObj = targetObj;
    this.sysUtils = new kendoUtils();
    this.init();
    return this;
}
InoutRecoderEdit.prototype = {
    init : function () {
        var fieldArray = this.sysUtils.getBindFieldArray(this.targetObj);
        var functionArray = [
            {fName : 'saveInfo', fs : this.saveInfo}
        ];
        this.models = new bindBaseModel(null, fieldArray, functionArray, null);
        this.sysUtils.bindDoucment(this.models, this.targetObj, this);
        this.loadInfo();
    },
    loadInfo : function () {
        var that = this;
        var editResult= window.showInoutRecodeView;
        var isEdit = false;
        if(typeof inoutRecoder !="undefined" &&isNotEmpt(inoutRecoder.isEdit)){
            isEdit = inoutRecoder.isEdit;
        }
        //var isEdit = isNotEmpt(inoutRecoder)?isNotEmpt(inoutRecoder.isEdit)? inoutRecoder.isEdit:null: null;
        if(isEdit){//
            that.targetObj.find('#vehicleNoSpan').removeAttr('disabled');
            that.targetObj.find('#vehicleNoSpan').removeClass('carNub');
            that.targetObj.find('#showSave').show();
        }else{
            that.targetObj.find('#showSave').hide();
        }
        var entity = editResult.entity;
        if (!isEmpty(entity)) {
            if(entity.recodeStatus==Utils.getModelValueByModelKey(BASIS.IN_OUT_STATUS.IN_OUT_STATUS_0)){//场内
                that.targetObj.find('#showOut').hide();
                that.targetObj.find('#showCharge').hide();
            }
            //支付平台
            //支付方式
            entity['payMethodName']=Utils.getModelNameByParentKey(BASIS.CHARGE_METHOD.ROOT,entity.payMethod);
            entity['credentialsName']=Utils.getModelNameByParentKey(BASIS.TRAFFIC_TYPE_CLASS.ROOT,entity.credentialsType);
            entity['platform']=Utils.getModelNameByParentKey(BASIS.PAY_PLATFORM.ROOT,entity.payPlatform);
            entity['recodeStatus']=Utils.getModelNameByParentKey(BASIS.IN_OUT_STATUS.ROOT,entity.recodeStatus);
            that.sysUtils.setBindFieldValue(entity);
        }
        //进出历史
        var steps=editResult.steps;
        if(!isEmpty(steps)){
            var data = new Array();
            for (var i = 0,l=steps.length; i < l; i++) {
                var step ={};
                if(steps[i].type=='out'){
                    step['应收金额']=steps[i].dueMoney;
                    step['实收金额']=steps[i].chargeMoney;
                    step['优惠金额']=steps[i].perMoney;
                }else{
                    step['应收金额']='--';
                    step['实收金额']='--';
                    step['优惠金额']='--';
                }
                if(steps[i].isPay==0){
                    step['支付状态']='未支付';
                }else if(steps[i].isPay==1){
                    step['支付状态']='已支付';
                }else{
                    step['支付状态']='--';
                }
                step['发生时间']=steps[i].inoutTime;
                step['通道名称']=steps[i].chnName == undefined ? '中央缴费':steps[i].chnName;
                data.push(step);
            }
            that.targetObj.find(".fishBone").fishBone(data);
        }else{
            that.targetObj.find(".fishBone").append('<div style="text-align:center;line-height:350px">未查到历史信息</div>');
        }
        //总计信息
        var countHtml='<li><label>应收总计：</label> <span data-bind="text:platform" style="color:#F00">'+entity.dueMoney+'</span>元</li>'
            +'         <li><label>实收总计：</label> <span data-bind="text:platform" style="color:#F00">'+entity.chargeMoney+'</span>元</li>'
            +'         <li><label>优惠总计：</label> <span data-bind="text:platform" style="color:#F00">'+entity.preMoney+'</span>元</li>';
            //+'         <li><label>超时收费金额：</label> <span data-bind="text:platform"  style="color:#F00">'+(entity.overtimesfje == undefined ? 0 : entity.overtimesfje)+'</span>元</li>'
            //+'         <li><label>预缴金额：</label> <span data-bind="text:platform"  style="color:#F00">'+(entity.advanceMoney == undefined ? 0 : entity.advanceMoney)+'</span>元</li>';
        that.targetObj.find("#count").append(countHtml);
        //优惠项目
        var pres=editResult.pres;
        var trHtml='';
        if(!isEmpty(pres)){
            for (var i = 0,l=pres.length; i < l; i++) {
                trHtml +='<tr><td>'+(pres[i].preferentialName == undefined ? '' : pres[i].preferentialName)+'</td>'
                    +'      <td>'+(pres[i].preChargeMoney == undefined ? '' : pres[i].preChargeMoney)+'</td>'
                    +'      <td>'+(pres[i].preMoney == undefined ? '' : pres[i].preMoney)+'</td>'
                    +'      <td>'+(pres[i].typeName == undefined ? '' : pres[i].typeName)+'</td>'
                    +'      <td>'+(pres[i].chargeName == undefined ? '' : pres[i].chargeName)+'</td>';
            }
        }else{
            trHtml='<tr><td colspan="5">本次消费无优惠项目</td></tr>';
        }
        that.targetObj.find("#preBody").append(trHtml);
        var inoutRecordImgs=editResult.inoutRecordImgs;
        var gpchtml='';
        if(!isEmpty(inoutRecordImgs)){
            var chns=new Array();
            for(var i= 0,l=inoutRecordImgs.length;i<l;i++){
                chns.push(inoutRecordImgs[i].channelCode)
            }
            Array.prototype.unique = function(){
                var res = [];
                var json = {};
                for(var i = 0; i < this.length; i++){
                    if(!json[this[i]]){
                        res.push(this[i]);
                        json[this[i]] = 1;
                    }
                }
                return res;
            }
            chns=chns.unique();

            for(var i=0,l=chns.length;i<l;i++){
                var chnName='',unitHtml='',imgHtml='';
                for(var m = 0,k=inoutRecordImgs.length; m < k; m++){
                    if(chns[i]==inoutRecordImgs[m].channelCode){
                        chnName=inoutRecordImgs[m].chnName == undefined ? '空':inoutRecordImgs[m].chnName;
                        if(isNotEmpt(inoutRecordImgs[m].imgUrl)){
                        	imgHtml+='<li><div class="pageContent"><div class="imgInfoBox"><img name="imageFullScreen" src="'+imgServerUrl+'/'+inoutRecordImgs[m].imgUrl.replace(/\\/g,"/")+'" ></div></div></li>';
                        }
                    }
                }
                if(imgHtml.length!=0){
                    unitHtml='<fieldset class="imgInfoStyle">'
                        +   '  <legend class="imgInfoName">'+chnName+'</legend>'
                        +   '  <div class="imgInfo">'
                        +   '      <ul id="images">'
                        +              imgHtml
                        +   '      </ul>'
                        +   '  </div>'
                        +   '</fieldset>';
                }
                if(unitHtml.length!=0){
                    gpchtml +=unitHtml;
                }
                if(gpchtml.length==0){
                    gpchtml='<div style="text-align:center;line-height:400px">未查到抓拍信息</div>';
                }
            }
        }else{
            gpchtml='<div style="text-align:center;line-height:400px">未查到抓拍信息</div>';
        }
        that.targetObj.find('#gpcimgs').append(gpchtml);
        var init = true;
        that.targetObj.find('.step-head').bind(
            'click',
            function(e) {
                if ($(this).hasClass('active')) {
                    return;
                }
                var no = $(this).attr('data-no');
                $('.step-head').removeClass('active');
                $('.step-head').removeClass('completed');

                $(this).prevAll().addClass('completed');
                $(this).addClass('active');
                $('.step-content .step-content-item').fadeOut(200);
                $('.step-content .step-content-item[data-on="' + no + '"]')
                    .fadeIn(200);
                if(no==3 && init){
                    that.targetObj.find('#gpcimgs').ImgLoading({
                        errorimg: "images/404-pic.png",
                        loadimg: "images/progressbar_microsoft.gif",
                        timeout: 1500,
                        targetObj:that.targetObj
                    });
                    init=false;
                }
            });
        if (that.targetObj.find('.step-head.active').length < 1) {
            that.targetObj.find('.step-head:eq(0)').click();
        }

        /*that.targetObj.find('img[name="imageFullScreen"]').each(function(){
        	$(this).smartZoom({'containerClass':'zoomableContainer'});
        }); */

    },
    saveInfo:function (e) {
        var that = this;
        if (!isEmpty(e)) {
            that = $(e.target).data('that');
        }
        if ( validityForm(that.targetObj) ){
            var param = that.sysUtils.getBindDocJson(that.models.bindFieldArray);

            JsonpAjax(baseApiPath + 'recordMarket/save', JSON.stringify(param), 'json', function (res) {
                closeWindow();
                inoutRecoder.isEdit = false;
                inoutRecoder.findData();
            }, true);
        }
    }
}